﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Teplomer
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private string dataReceived = String.Empty;
    private bool showData = false;
    private bool isConnected = false;
    private bool isMini = false;

    private void Form1_Load(object sender, EventArgs e)
    {
      string[] ports = SerialPort.GetPortNames();
      foreach (string port in ports) {
        comboBox1.Items.Add(port);
      }
    }


    private void connectPort()
    {
      try
      {
        serialPort1.PortName = comboBox1.Text;
        serialPort1.Open();
        timer1.Start();
      }
      catch (Exception ex)
      {
        label1.Text = ex.Message;
      }
    }

    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {

      SerialPort sp = (SerialPort)sender;
      dataReceived += sp.ReadExisting();
      if (dataReceived.Contains("\r\n"))
      {
        showData = true;
      }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
      if (showData)
      {
        dataReceived = dataReceived.Replace("\n", "").Replace("\r", "");
        label1.Text = dataReceived;
        dataReceived = dataReceived.Replace(".",",");
        double d = 0.0;
        try { 
          d = Convert.ToDouble(dataReceived);
        }
        catch (Exception) { 
          d = 0;
        }  

        label1.ForeColor = Color.LightBlue;
        if (d >= 15.0) { label1.ForeColor = Color.Blue; }
        if (d >= 20.0) { label1.ForeColor = Color.Green; }
        if (d >= 23.0) { label1.ForeColor = Color.OrangeRed; }
        if (d >= 25.0) { label1.ForeColor = Color.Red; }
        dataReceived = String.Empty;
      }
      showData = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
      serialPort1.Close();
      Application.Exit();
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
      CheckBox cb = (CheckBox)sender;
      this.TopMost = cb.Checked;
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
      if (!isConnected) {
        if (comboBox1.Text == String.Empty) { return;}
        connectPort();
        button1.Text = "Odpojit";
        isConnected = true;
        button2.Visible = true;
      } else {
        serialPort1.Close();
        isConnected = false;
        button1.Text = "Připojit";
        label1.Text = String.Empty;
        showData = false;
        button2.Visible = false;
      }
    }

    private void button2_Click(object sender, EventArgs e)
    {
      if (!isMini) {
        label1.Font = new Font("LcdD", 15);
        isMini = true;
        button2.Text = "Maxi";
        this.Size = new Size(135, 65);
        button2.Location = new Point(75, 0);
      }
      else {
        label1.Font = new Font("LcdD", 99);
        isMini = false;
        button2.Text = "Mini";
        this.Size = new Size(376, 213);
        button2.Location = new Point(306, 140);
      }
    }

    private void trackBar1_Scroll(object sender, EventArgs e)
    {
      TrackBar tb = (TrackBar)sender;
      this.Opacity = tb.Value * 0.01;
    }
  }
}
